<?php

/**
 * 供应链模块
 */
namespace app\schain\controller;

use app\schain\model\SchainEnterpriseModel;
use app\schain\model\SchainTagModel;
use app\schain\service\EnterpriseService;
use cmf\controller\HomeBaseController;
use think\Db;

class EnterpriseController extends HomeBaseController {

	/**
	 * 企业详情
	 * @return [type] [description]
	 */
	public function index() {
		$ent_id = $this->request->param('id', 0, 'intval');
		$category_id = $this->request->param('cid', 0, 'intval');

		$enterpriseService = new EnterpriseService();

		$enterprise = $enterpriseService->publishedEnterprise($ent_id, $category_id);

		if (empty($enterprise['id'])) {
			abort(404, '内容不存在!或者未审核通过。');
		}

		$prevEnterprise = $enterpriseService->publishedPrevEnterprise($ent_id, $category_id);
		$nextEnterprise = $enterpriseService->publishedNextEnterprise($ent_id, $category_id);

		//浏览数增加
		Db::name('schain_enterprise')->where(['id' => $ent_id])->setInc('ent_hits');

		//查询发布者
		$enterprise['user'] = Db::name('user')->where(['id' => $enterprise['user_id']])->find();

		//查询行业
		$schainEnterpriseModel = new SchainEnterpriseModel();
		$post = $schainEnterpriseModel->where('id', $ent_id)->find();

		$cardCategories = $post->categories()->alias('a')->column('a.name', 'a.id');
		$cardCategoryIds = implode(',', array_keys($cardCategories));

		$enterprise['ent_category'] = implode(',', array_values($cardCategories));

		$this->assign('enterprise', $enterprise);
		$this->assign('prev_enterprise', $prevEnterprise);
		$this->assign('next_enterprise', $nextEnterprise);

		return $this->fetch(':detail');
	}

	/**
	 * 添加企业
	 */
	public function add() {
		$this->checkUserLogin();
		//加载标签
		$schainTagModel = new SchainTagModel();
		$tags = $schainTagModel->select();
		$this->assign("tags", $tags);

		return $this->fetch(':add');
	}

	/**
	 * 添加企业提交
	 */
	public function addPost() {
		if ($this->request->isPost()) {
			$data = $this->request->param();
			$post = $data['post'];
			$result = $this->validate($post, 'AdminEnterprise');
			if ($result !== true) {
				$this->error($result);
			}

			$schainEnterpriseModel = new SchainEnterpriseModel();

			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
				$data['post']['more']['photos'] = [];
				foreach ($data['photo_urls'] as $key => $url) {
					$photoUrl = cmf_asset_relative_url($url);
					array_push($data['post']['more']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
				}
			}

			if (!empty($data['file_names']) && !empty($data['file_urls'])) {
				$data['post']['more']['files'] = [];
				foreach ($data['file_urls'] as $key => $url) {
					$fileUrl = cmf_asset_relative_url($url);
					array_push($data['post']['more']['files'], ["url" => $fileUrl, "name" => $data['file_names'][$key]]);
				}
			}

			//提交后自动发布
			$data['post']['published_time'] = date('Y-m-d H:i:s', time());

			//前台用户自行添加名片时，作者是当前登录的用户
			$data['post']['user_id'] = cmf_get_current_user_id();

			$schainEnterpriseModel->adminAddEnterprise($data['post'], $data['post']['categories']);

			$data['post']['id'] = $schainEnterpriseModel->id;

			$this->success('添加成功!', url('schain/Enterprise/index', ['id' => $schainEnterpriseModel->id]));
		}
	}

	// 企业点赞
	public function doLike() {
		$this->checkUserLogin();
		$articleId = $this->request->param('id', 0, 'intval');

		$canLike = cmf_check_user_action("enterprise$articleId", 1);

		if ($canLike) {
			Db::name('schain_enterprise')->where(['id' => $articleId])->setInc('ent_like');

			$this->success("赞好啦！");
		} else {
			$this->error("您已赞过啦！");
		}
	}
}
